<template>
    <!-- 表格组件 -->
    <el-table :data="tableData" border>
        <el-table-column label="日期">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <el-icon>
                        <timer />
                    </el-icon>
                    <span style="margin-left: 10px">{{ scope.row.signin_create_time }}</span>
                </div>
            </template>
        </el-table-column>

        <el-table-column label="文章回复">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <el-icon>
                        <Avatar />
                    </el-icon>
                    <span style="margin-left: 10px">{{ scope.row.reply_article_message }}</span>
                </div>
            </template>
        </el-table-column>

        <el-table-column>
            <template #header>操作</template>
            <template #default="scope">
                <!-- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
                <el-button size="small" @click="handleDelete(scope.$index, scope.row)" type="danger">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="margin-top:20px">
        <el-pagination layout="prev, pager, next" :total="total2" @prev-click="handlePrevChange"
            @next-click="handleNextChange" @current-change="handleCurrentChange" />
    </div>
</template>

<script lang="ts" setup>
// 导入消息提示框
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { replyArticleAllApi, replyArticleRemoveApi, replyArticleItemsApi } from '../../http/request';
import moment from "moment";
// 导入创建路由方法
import { useRouter } from 'vue-router';
// 定义路由
const $router = useRouter();
// 用户临时身份token
let token = localStorage.getItem('token');
// 定义数据结构
interface ReplyArticle {
    reply_create_time: string
    reply_article_message: string,
    reply_article_id: string,
}
// 记录数据
let tableData: ReplyArticle[] = reactive([]);

// 交互行为2
const handleDelete = (index: number, row: ReplyArticle) => {
    // 获取内容id
    let reply_article_id = row.reply_article_id;
    // 删除内容
    replyArticleRemoveApi({ token, reply_article_id })
        .then(
            (data: any) => {
                if (data.code == 200) {
                    tableData.splice(index, 1)
                }
                // 提示
                ElMessage({
                    message: data.msg,
                    type: 'warning',
                });
            }
        )
}
// 初始化内容列表
const initReplyArticleList = async (page_num: number | any, page_size: number | any) => {
    // 调用内容列表接口
    await replyArticleItemsApi({ page_num, page_size })
        .then(
            (data: any) => {
                if (data.code == 200) {
                    // 重置表格数据
                    tableData.splice(0, tableData.length);
                    // 解构赋值
                    let { result } = data;
                    // 整合数据
                    result.map((item: any) => {
                        if (item.reply_create_time) {
                            item.reply_create_time = moment(new Date(item.reply_create_time)).format('YYYY-MM-DD hh:mm:ss')
                        }
                        tableData.push(item)
                    });
                }
            }
        )
}

// 初始化 
initReplyArticleList(1, 10);

// 分页处理
const handleCurrentChange = (val: number) => {
    //   console.log(`current page: ${val}`)
    initReplyArticleList(val, 10);
}
const handlePrevChange = (val: number) => {
    //   console.log(`prev page: ${val}`)
    initReplyArticleList(val, 10);
}
const handleNextChange = (val: number) => {
    //   console.log(`next page: ${val}`)
    initReplyArticleList(val, 10);
}

// 统计数量
let total2 = ref(50);
replyArticleAllApi()
    .then(
        data => {
            if (data.code == 200) {
                //console.log(data)
                total2.value = data.total;
            }
        }
    )
</script>